﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class _Sizeandlayoutofdrawingsheets
    {
        public static void Sizeandlayoutofdrawingsheets(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            string str = Convert.ToString(data["str"]);
            string[] strs = str.Split('×');
            double b = 0;
            double l = 0;

            if (str == "0")
            {
                b = 841;
                l = 1189;
            }
            if (str == "1")
            {
                b = 594;
                l = 841;
            }
            if (str == "2")
            {
                b = 420;
                l = 594;
            }
            if (str == "3")
            {
                b = 297;
                l = 420;
            }
            if (str == "4")
            {
                b = 210;
                l = 297;
            }
            if (str == "3×3")
            {
                b = 420;
                l = 891;
            }
            if (str == "3×4")
            {
                b = 420;
                l = 1189;
            }
            if (str == "4×3")
            {
                b = 297;
                l = 630;
            }
            if (str == "4×4")
            {
                b = 297;
                l = 841;
            }
            if (str == "4×5")
            {
                b = 297;
                l = 1051;
            }
            if (str == "0×2")
            {
                b = 1189;
                l = 1682;
            }
            if (str == "0×3")
            {
                b = 1189;
                l = 2523;
            }
            if (str == "1×3")
            {
                b = 841;
                l = 1783;
            }
            if (str == "1×4")
            {
                b = 841;
                l = 2378;
            }
            if (str == "2×3")
            {
                b = 594;
                l = 1261;
            }
            if (str == "2×4")
            {
                b = 594;
                l = 1682;
            }
            if (str == "2×5")
            {
                b = 591;
                l = 2102;
            }
            if (str == "3×5")
            {
                b = 420;
                l = 1486;
            }
            if (str == "3×6")
            {
                b = 420;
                l = 1783;
            }
            if (str == "3×7")
            {
                b = 420;
                l = 2080;
            }
            if (str == "4×6")
            {
                b = 297;
                l = 1261;
            }
            if (str == "4×7")
            {
                b = 297;
                l = 1471;
            }
            if (str == "4×8")
            {
                b = 297;
                l = 1682;
            }
            if (str == "4×9")
            {
                b = 297;
                l = 1892;
            }

            if (str == "0" | str == "4" | str == "4×4" | str == "1×3" | str == "1×4" | str == "3×5" | str == "3×6" | str == "3×7")
            {
                Vector2 v1 = new Vector2(0, 0);
                Vector2 v2 = new Vector2(v1.X + l, v1.Y);
                Vector2 v3 = new Vector2(v2.X, v2.Y + b);
                Vector2 v4 = new Vector2(v1.X, v3.Y);



                LwPolyline line1 = new LwPolyline();
                line1.Vertexes.Add(new LwPolylineVertex(v1));
                line1.Vertexes.Add(new LwPolylineVertex(v2));
                line1.Vertexes.Add(new LwPolylineVertex(v3));
                line1.Vertexes.Add(new LwPolylineVertex(v4));
                line1.Vertexes.Add(new LwPolylineVertex(v1));
                fun.PolyFill(dxf, line1, 0.5, contour);


            }
            else
            {
                Vector2 v1 = new Vector2(0, 0);
                Vector2 v2 = new Vector2(v1.X + b, v1.Y);
                Vector2 v3 = new Vector2(v2.X, v2.Y + l);
                Vector2 v4 = new Vector2(v1.X, v3.Y);



                LwPolyline line1 = new LwPolyline();
                line1.Vertexes.Add(new LwPolylineVertex(v1));
                line1.Vertexes.Add(new LwPolylineVertex(v2));
                line1.Vertexes.Add(new LwPolylineVertex(v3));
                line1.Vertexes.Add(new LwPolylineVertex(v4));
                line1.Vertexes.Add(new LwPolylineVertex(v1));
                fun.PolyFill(dxf, line1, 0.5, contour);

            }

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
